package com.hxy.org.controller;

import com.hxy.basic.query.util.AjaxResult;
import com.hxy.basic.query.util.PageList;
import com.hxy.org.domain.Employee;
import com.hxy.org.query.EmployeeQuery;
import com.hxy.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 员工接口, controller控制层
 */
@RestController // @Controller+@ResponseBody两个注解的结合
@RequestMapping("/employee")
@Api(tags = "员工接口",description = "员工接口详细描述") // swagger注解,为了让前端开发能够看懂
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    /**
     * 查询所有
     * localhost:8080/employee
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有",notes = "") // swagger注解,为了让前端开发能够看懂
    public List<Employee> findAll() {
        return employeeService.findAll();
    }

    /**
     * 查询一个
     * @param id
     * @return
     * @GetMapping 本质上是 localhost:8080/employee/id = localhost:8080/employee"/{id}"
     * {}区分参数
     * 通过注解@PathVariable获取URL地址中的数据指定为("id")赋值给Long id
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询一个对象",notes = "传入id")
    public Employee findOne(@PathVariable("id") Long id) {
        return employeeService.findOne(id);
    }

    /**
     * 分页查询
     * 一般获取会用GetMapping 但是分页查询的参数过多. 就使用了PostMapping
     * 可以通过@RequestBody获取请求体中的EmployeeQuery数据
     *
     * @param employeeQuery
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询",notes = "传入分页参数")
    public PageList<Employee> queryList(@RequestBody EmployeeQuery employeeQuery) {
        return employeeService.queryPage(employeeQuery);
    }


    /**
     * 删除
     * 使用注解@DeleteMapping 获取参数与上面'查询一个'相同
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除",notes = "传入id")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            // 调用删除方法
            employeeService.delete(id);
            return new AjaxResult(); // 默认Success为true,msg操作成功
        } catch (Exception e) {
            e.printStackTrace();
            // 链式编程,失败需要赋值Success与msg
            return AjaxResult.me().setSuccess(false).setMsg("删除失败," + e.getMessage());
        }
    }


    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除",notes = "需要传递删除的id")
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            employeeService.batchRemove(ids);
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("批量删除失败" + e.getMessage());
        }
    }

    /**
     * 修改与添加
     * 使用@PutMapping注解
     * 参数获取方法与上面'分页查询'相同
     * @param employee
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加和修改",notes = "传入id就是修改,不传id就是添加")
    public AjaxResult saveOrUpdate(@RequestBody Employee employee) {
        try {
            if(employee.getId() == null){ // 没有id就是添加
                employeeService.save(employee);
            } else {
                employeeService.update(employee); // 有就是修改
            }
            return new AjaxResult(); // 默认Success为true,msg操作成功
        } catch (Exception e) {
            e.printStackTrace();
            // 链式编程,失败需要赋值Success与msg
            return AjaxResult.me().setSuccess(false).setMsg("操作失败," + e.getMessage());
        }
    }
}
